﻿using Oracle.DataAccess.Client;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TechTree.Stalk.Roots.Database;

namespace TechTree.Stalk.Roots.Databases.Oracle
{
    public class OracleDatabaseContext : DatabaseContext
    {
        public OracleDatabaseContext(string connectionString)
        {
            base.ConnectionString = connectionString;
        }

        public override IDbConnection CreateConnection()
        {
            return new OracleConnection(ConnectionString);
        }
        public override IDbCommand CreateCommand()
        {
            return new SqlCommand();
        }
        public override IDbConnection CreateOpenConnection()
        {
            OracleConnection connection = (OracleConnection)CreateConnection();
            connection.Open();

            return connection;
        }
        public override IDbCommand CreateCommand(string commandText, IDbConnection connection)
        {
            OracleCommand command = (OracleCommand)CreateCommand();

            command.CommandText = commandText;
            command.Connection = (OracleConnection)connection;
            command.CommandType = CommandType.Text;

            return command;
        }
        public override IDbCommand CreateStoredProcCommand(string procName, IDbConnection connection)
        {
            OracleCommand command = (OracleCommand)CreateCommand();

            command.CommandText = procName;
            command.Connection = (OracleConnection)connection;
            command.CommandType = CommandType.StoredProcedure;

            return command;
        }
        public override IDataParameter CreateParameter(string parameterName, object parameterValue)
        {
            return this.CreateParameter(parameterName, DbType.String, 0, null);
        }
        public override IDataParameter CreateParameter(string parameterName, DbType dbType, object value)
        {
            return this.CreateParameter(parameterName, dbType, 0, value);
        }
        public override IDataParameter CreateParameter(string parameterName, DbType dbType, int size, object value)
        {
            return this.CreateParameter(parameterName, dbType, size, null, value);
        }
        public override IDataParameter CreateParameter(string parameterName, DbType dbType, int size, string sourceColumn, object value)
        {
            return this.CreateParameter(parameterName, dbType, size, ParameterDirection.Input, true, sourceColumn, DataRowVersion.Default, value);
        }
        public override IDataParameter CreateParameter(string parameterName, DbType dbType, int size, ParameterDirection direction, bool isNullable, string sourceColumn, DataRowVersion sourceVersion, object value)
        {
            OracleParameter sqlParameter = new OracleParameter();
            sqlParameter.ParameterName = parameterName;
            sqlParameter.DbType = DbType.String;
            sqlParameter.Size = size;
            sqlParameter.Direction = direction;
            sqlParameter.SourceColumnNullMapping = isNullable;
            sqlParameter.SourceColumn = sourceColumn;
            sqlParameter.SourceVersion = sourceVersion;
            sqlParameter.Value = value;

            return sqlParameter;
        }
    }
}
